package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import com.example.service.impl.MailServiceIml;
import com.example.util.verifyCodeCreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * @copyright (C), 2019-2022, Kkoo
 * @author: kkoo
 * @date: 2022/5/16 9:26 PM
 * @fileName: UserController
 */

@Controller
@RequestMapping("/mail")
public class MailController {

    @Autowired
    private MailServiceIml mailSender;
    @Autowired
    private UserService userService;

    @RequestMapping("/send")
    public String mailSend(String email, ModelMap map) {
        User user = userService.edit(email);
        if (user != null) {
            user.setEmailCode(verifyCodeCreate.creatCode(6));
            user.setVerifyCodeCreateTime(String.valueOf(System.currentTimeMillis()));
            userService.saveCode(user);
            mailSender.sendMail(user.getEmail(), "请回暗号", user.getEmailCode());
            return "发送成功";
        }
        return "发送失败";
    }

    @RequestMapping("/login")
    public String login(String email, String emailCode, ModelMap map) {
        User user = userService.edit(email);
        if (user != null) {
            if (user.getEmailCode().equals(emailCode)) {
                return "index1";
            }
            return "index";
        }
        return "index";
    }
}